package net.oschina.simpleplus.gen;

import org.apache.commons.lang3.StringUtils;

/**
 * 针对 repeated 字段
 */
public class ProtoFieldList extends ProtoField{

	public ProtoFieldList() {
		super();
		
	}

	public ProtoFieldList(String name, String javaType) {
		super(name, javaType);
	}

	@Override
	public StringBuffer appendField(StringBuffer buffer) {
		return buffer.append("\n\t")
				.append("private List<")
				.append(javaType)
				.append("> ")
				.append(name)
				.append(";");
	}

	@Override
	public StringBuffer appendGetterMethod(StringBuffer buffer) {
		return buffer.append("\n\t")
		.append("public List<")
		.append(javaType)
		.append("> get")
		.append(StringUtils.capitalize(name))
		.append("(){\n\t\t")
		.append("return this.")
		.append(name)
		.append(";\n\t}");
	}

	@Override
	public StringBuffer appendSetterMethod(StringBuffer buffer) {
		return buffer.append("\n\t")
				.append("public ")
				.append("void ")
				.append("set")
				.append(StringUtils.capitalize(name))
				.append("(List<")
				.append(javaType)
				.append("> ")
				.append(name)
				.append("){\n\t\t")
				.append("this.")
				.append(name)
				.append(" = ")
				.append(name)
				.append(";\n\t}");
	}

	@Override
	public StringBuffer appendBuildMethod(StringBuffer buffer) {
		if("byte[]".equals(javaType)){
			return buffer.append("\n\t\t")
						.append("builder.addAll")
						.append(StringUtils.capitalize(name))
						.append("(ConvertUtil.covert2ByteString(")
						.append(name)
						.append("));"); 
		}
		return buffer.append("\n\t\t")
		.append("builder.addAll")
		.append(StringUtils.capitalize(name))
		.append("(")
		.append(name)
		.append(");");
	}

	@Override
	public StringBuffer appendCopyFromMethod(StringBuffer buffer) {
		if("byte[]".equals(javaType)){
			return buffer.append("\n\t\t")
					.append("this.")
					.append(name)
					.append(".addAll(ConvertUtil.covert2bytes(t.get")
					.append(StringUtils.capitalize(name))
					.append("List()));");
		}
		return buffer.append("\n\t\t")
				.append("this.")
				.append(name)
				.append(".addAll(t.get")
				.append(StringUtils.capitalize(name))
				.append("List());");
	}

	@Override
	protected String defaultInitlized() {
		return new StringBuffer().append("new ArrayList<").append(javaType).append(">()").toString();
	}

}
